package me.firstapp.service.shop.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.shop.Category;
import me.firstapp.repository.shop.CategoryRepository;
import me.firstapp.service.shop.CategoryService;

@Service
public class CategoryServiceImpl implements CategoryService {

	@Autowired
	private CategoryRepository categoryRepository;

	public List<Category> findCategories(Long apiClientId) {
		return categoryRepository.findCategories(apiClientId);
	}

	@Override
	public Page<Category> findSectionPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType, Long apiClientId) {
		return categoryRepository.findSectionPage(pageNo, pageSize, searchParams, sortParam, sortType, apiClientId);
	}

	@Override
	public Category findCategoryById(Long categoryId, Long apiClientId) {
		return categoryRepository.findCategoryById(categoryId, apiClientId);
	}

	@Override
	public Category addCategory(String name, String description, Integer sort, Long apiClientId) {
		if (StrUtils.isNULL(name)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "名称");
		}
		if (sort == null) {
			sort = 0;
		}
		Category category = new Category();
		category.setName(name);
		category.setDescription(description);
		category.setSort(sort);
		category.setApiClientId(apiClientId);
		categoryRepository.save(category);
		return category;
	}

	@Override
	public Category updateCategory(Long categoryId, String name, String description, Integer sort, Long apiClientId) {
		if (categoryId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "品类id");
		}
		Category category = categoryRepository.findCategoryById(categoryId, apiClientId);
		if (category == null) {
			throw new ServiceException(StatusHouse.CATEGORY_NOT_FOUND);
		}
		if (StrUtils.isNotNULL(name)) {
			category.setName(name);
		}
		if (StrUtils.isNotNULL(description)) {
			category.setDescription(description);
		}
		if (sort != null) {
			category.setSort(sort);
		}
		categoryRepository.update(category);
		return category;
	}

	@Override
	public void deleteCategorys(String ids, Long apiClientId) {
		if (StrUtils.isNULL(ids)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "品类id");
		}
		if (!ids.contains(",")) {
			Category category = categoryRepository.findCategoryById(Long.valueOf(ids), apiClientId);
			if (category != null) {
				categoryRepository.deleteById(Long.valueOf(ids));
			}
		} else {
			String[] _ids = ids.split(",");
			Long[] _ids_ = new Long[_ids.length];
			for (int i = 0; i < _ids.length; i++) {
				Category category = categoryRepository.findCategoryById(Long.valueOf(_ids[i]), apiClientId);
				if (category != null) {
					_ids_[i] = Long.valueOf(_ids[i]);
				}

			}
			if (_ids.length > 0) {
				categoryRepository.delete(_ids_);
			}
		}

	}

}
